﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Net.Mail;

namespace DATN.mds.MailManagement
{
    public partial class GroupMail : System.Web.UI.Page
    {
        string cnnString;
        SqlConnection cnn;
        SqlCommand cmd;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["Us_Id"] != null)
                {
                    BindDrr();
                }
                else
                {
                    Response.Redirect("../Account/Login.aspx");
                }
                Validate();
            }
        }

        //Bind Data to DropDownList
        protected void BindDrr() {
            try
            {
                cnnString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
                cnn = new SqlConnection(cnnString);

                string stringQuery = "SELECT * FROM GroupCustomer";
                SqlCommand cmd = new SqlCommand(stringQuery,cnn);
                cmd.Connection.Open();

                SqlDataReader ddr;
                ddr = cmd.ExecuteReader();

                ddr_GroupCS.DataSource = ddr;
                ddr_GroupCS.DataValueField = "GrCus_Id";
                ddr_GroupCS.DataTextField = "Name";
                ddr_GroupCS.DataBind();

                cmd.Connection.Close();
                cmd.Connection.Dispose();
            }
            catch (Exception ex)
            {
                Label4.Text = "Lỗi" + ex;
            }
        }

        String ListMail= "";
        int DrrSelected;
        //bt Gửi Mail Chọn Từ Drr
        protected void Button1_Click(object sender, EventArgs e)
        {
            //Check Mail Gửi
            DrrSelected = Convert.ToInt16(ddr_GroupCS.SelectedValue);
            SqlDataReader rdr = null;
            try
            {
                string cnnString = ConfigurationManager.ConnectionStrings["DATNconnection"].ConnectionString;
                cnn = new SqlConnection(cnnString);
                cnn.Open();

                String queryString = "SELECT Mail FROM Customers WHERE GrCus_Id = @GrCus_Id";
                cmd = new SqlCommand(queryString, cnn);
                cmd.Parameters.Add("@GrCus_Id", SqlDbType.Int);
                cmd.Parameters["@GrCus_Id"].Value = DrrSelected;

                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    ListMail += (string)rdr["Mail"] + "; ";
                }
            }
            catch (Exception ex)
            {
                Response.Write(" Lỗi ở Check Mail: " + ex);
            }
            //Send Mail
            SendMail(ListMail);
        } 

        //bt Bỏ qua
        protected void bt_Cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("../mds.UserInterface/InternalIndex.aspx");
        }

        //Gửi mail
        protected void SendMail(string ListMail)
        {
               
        }
    }
}